<template>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card>
        <el-row :gutter="20">
          <!--输入框-->
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" @clear="getGoodsList" clearable >
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="3">
            <el-button type="primary" @click="goAddpage">添加用户</el-button>
          </el-col>
        </el-row>
        <!--表格-->
        <el-table :data="goodsList" style="width: 100%" border>
          <el-table-column type="index" label="#" ></el-table-column>
          <el-table-column prop="goods_name" label="商品名称" ></el-table-column>
          <el-table-column prop="goods_number" label="数量" width="70px"></el-table-column>
          <el-table-column prop="goods_price"  label="价格（元）" width="95px"></el-table-column>
          <el-table-column prop="goods_weight"  label="重量（kg）" width="95px"></el-table-column>
          <el-table-column prop="add_time"  label="时间" width="110px">
            <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
          </el-table-column>
          <el-table-column label="操作" width="130px">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" icon="el-icon-edit"  @click="showEditGoodsDialog(scope.row.goods_id)"></el-button>
              <el-button size="mini" type="danger" icon="el-icon-delete" @click="removeGoodsById(scope.row.goods_id)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="pageSizes"
          :page-size="queryInfo.pagesize"
          :layout="layout"
          :total="total" background>
        </el-pagination>
      </el-card>
    </div>
</template>

<script>
    export default {
        data() {
            return {
              goodsList: [],
              queryInfo: {
                query: '', // 查询参数
                pagenum: 1, // 当前页码
                pagesize: 10 //  每页显示条数
              },
              pageSizes: [10, 15, 20],
              layout: 'total, sizes, prev, pager, next, jumper',
              total: 0
            }
        },
        created() {
          this.getGoodsList()
        },
        methods: {
          async getGoodsList() {
            const {data: res} = await this.$http.get('goods', {
              params: this.queryInfo
            });
            if (res.meta.status !== 200) return this.$message.error({message: '获取商品列表失败！'});
            this.goodsList = res.data.goods;
            this.total = res.data.total
          },
          handleSizeChange(newSize) {
            this.queryInfo.pagesize = newSize
            this.getGoodsList()
          },
          handleCurrentChange(newPage) {
            this.queryInfo.pagenum = newPage
            this.getGoodsList()
          },
          // 跳转添加页面
          goAddpage() {
            this.$router.push('/goods/add')
          },
          // 编辑商品
          showEditGoodsDialog(id) {

          },
          // 删除商品
          async removeGoodsById(id) {
            const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(err => err);
            // 确定返回字符串 confirm ，取消返回字符串 cancel
            if (confirmResult === 'cancel') {
              return this.$message.info('已经取消删除！');
            }
            console.log('确定删除')
            const {data: res} = await this.$http.delete(`goods/${id}`)
            if (res.meta.status !== 200) return this.$message.error({message: '删除用户失败！'});
            this.$message.success({message: '删除用户成功！'});
            this.getGoodsList()
          }
        }
    }
</script>

<style scoped>

</style>
